package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;

import com.echoesnet.echoesshortvideo.Api.ApiException;
import com.echoesnet.echoesshortvideo.Api.ErrorCodeTable;
import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Beans.ErroInfoBean;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Repositories.LoginRepository;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import io.reactivex.Observer;

/**
 * Created by Administrator on 2018/4/23.
 *
 * @author ling
 */
public class LoginViewModel
{
    //MediatorLiveData
    private LoginRepository loginRepository;

    public LiveData<String> getLoginLive()
    {
        return loginLive;
    }

    private MutableLiveData<String> loginLive = new MutableLiveData<>();

    public MutableLiveData<ErroInfoBean> getLoginErroerLive()
    {
        return loginErroerLive;
    }

    private MutableLiveData<ErroInfoBean> loginErroerLive = new MutableLiveData<>();

    public LoginViewModel()
    {
        loginRepository = new LoginRepository();
    }

    public void checkUserRegisterState(String phoneNum)
    {
        if (loginRepository == null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.checkUserRegisterState(phoneNum, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    String type = jb.optString("type", "0");
                    loginLive.setValue(type);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                loginErroerLive.setValue(new ErroInfoBean(Api.LOGINC_CHECK_MOBILE,apiE.getErrorCode(),apiE.getErrBody()));
            }
        });
    }

    public void findPassWord(String phone, String verlfyCode, String password)
    {
        if (loginRepository == null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.findPassWord(phone, verlfyCode, password, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    String type = jb.optString("type", "0");
                    loginLive.setValue(type);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                ErroInfoBean erroInfoBean = new ErroInfoBean(Api.LOGINC_FORGETPASSWORD, apiE.getErrorCode(), apiE.getErrBody());
                loginErroerLive.setValue(erroInfoBean);
            }
        });

    }

    public void alertPassWord(String password, String newPassword)
    {
        if (loginRepository == null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.alertPassWord(password, newPassword, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    String type = jb.optString("type", "0");
                    loginLive.setValue(type);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                if(!apiE.getErrorCode().equals(ErrorCodeTable.PASSWORD_ERROR))
                super.onHandledError(apiE);
                else
                {
                    ErroInfoBean erroInfoBean = new ErroInfoBean(Api.LOGINC_MODIFY_PASSWORD, apiE.getErrorCode(), apiE.getErrBody());
                    loginErroerLive.setValue(erroInfoBean);
                }
            }
        });

    }

    public void login(String phone, String password)
    {
        if (loginRepository == null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.login(phone, password, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    SharePreUtils.setUId(jb.getString("userId"));
                    SharePreUtils.setToken(jb.getString("token"));
                    loginLive.setValue("ok");
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }


            @Override
            public void onHandledError(ApiException apiE)
            {
                ErroInfoBean erroInfoBean = new ErroInfoBean(Api.LOGINC_LOGIN, apiE.getErrorCode(), apiE.getErrBody());
                loginErroerLive.setValue(erroInfoBean);
                super.onHandledError(apiE);

            }
        });
    }
    public void wexinLogin(Platform platform)
    {
        if (loginRepository == null)
            throw new NullPointerException("LoginRepository不可为空");
        loginRepository.weixinLogin(platform, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jb = new JSONObject(response);
                    SharePreUtils.setUId(jb.getString("userId"));
                    SharePreUtils.setToken(jb.getString("token"));
                    loginLive.setValue("ok");
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }


            @Override
            public void onHandledError(ApiException apiE)
            {
                ErroInfoBean erroInfoBean = new ErroInfoBean(Api.LOGINC_LOGIN, apiE.getErrorCode(), apiE.getErrBody());
                loginErroerLive.setValue(erroInfoBean);
                super.onHandledError(apiE);

            }
        });
    }

}
